/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.incremental;

import static gui.incremental.TransitionObservable.MODIF_TYPE.CLOCK_GUARD;
import static gui.incremental.TransitionObservable.MODIF_TYPE.DEST_LOCATION;
import static gui.incremental.TransitionObservable.MODIF_TYPE.MESSAGE;
import static gui.incremental.TransitionObservable.MODIF_TYPE.RESET_CLOCK;
import static gui.incremental.TransitionObservable.MODIF_TYPE.SRC_LOCATION;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Observable;
import javax.swing.JComboBox;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import semantics.TIOARuntime;
import semantics.reachability.IncrementalReachabilityAnalysis;
import semantics.reachability.ReachabilityAnalysis;
import syntax.Clock;
import syntax.ClockConditionAtom;
import syntax.InternalAction;
import syntax.Location;
import syntax.Message;
import syntax.Repository;
import syntax.TIOA;
import syntax.Transition;

/**
 *
 * @author ennouaary
 */
public class JTIOADialog extends TIOAObservableJDialog {

    TIOA system, origSystem;
    Collection<Message> messages;
    Collection<Location> locations;
    Collection<Transition> transitions;
    Collection<Clock> clocks;
    Repository repositoryOrig = Repository.getInstance();
    InternalAction internalAction = repositoryOrig.getInternalAction();
    Transition transition;
    Location location;
    Message message;
    Clock clock;
    Repository repository;

    private void restore() {
//        this.transition = (Transition) (origTransition.clone());
        initCollections();
        fillInComponents();
    }

    private void initCollections() {
        locations = repository.getAllLocations();
        messages = repository.getAllMessages();
        clocks = repository.getAllClocks();
//        locations = new LinkedList<Location>(repository.getAllLocations());
//        messages = new LinkedList<Message>(repository.getAllMessages());
//        clocks = new LinkedList<Clock>(repository.getAllClocks());
    }

    private void fillInComponents() {
        int infinity = (int) Math.floor(parameters.Parameters.getInfinity());
//        jSourceLocationComboBox.setModel(null);
        //fillInClockGuardAtoms();
        fillInSystemName();
        fillInMessages();
        fillInLocations();
        fillInTransitions();
        fillInClocks();
    }

    private void fillInSystemName() {
        jSystemNameTextField.setText(system.getName());
    }

    private void fillInClocks() {
        fillInObjects(jClocksComboBox, clocks);
        //Collections.sort((List<Clock>) clocks);
    }

    private void fillInMessages() {
        fillInObjects(jMessagesComboBox, messages);
//        if (messages.size() <= 1) {
//            jMessageDeleteButton.setEnabled(false);
//            jMessageEditButton.setEnabled(false);
//        } else {
//            jMessageDeleteButton.setEnabled(true);
//            jMessageEditButton.setEnabled(true);
//        }
        //Collections.sort((messages);
    }

    private void fillInLocations() {
//        fillInObjects(jSourceLocationComboBox, repository.getAllLocations(), transition.getSrcLocation());
        fillInObjects(jLocationsComboBox, locations);
        //Collections.sort((List<Location>) locations);
    }

    private void fillInTransitions() {
//        fillInObjects(jSourceLocationComboBox, repository.getAllLocations(), transition.getSrcLocation());
        fillInObjects(jTransitionsComboBox, transitions);
        //Collections.sort((List<Transition>) transitions);
    }

    private void fillInObjects(JComboBox combo, Collection collection) {
        List objects = new LinkedList(collection);
        Collections.sort(objects);
        combo.removeAllItems();
        for (Object object : objects) {
            combo.addItem(object);
        }
    }

    private void fillInObjects(JComboBox combo, Collection collection, Object selected) {
        List objects = new LinkedList(collection);
        Collections.sort(objects);
        combo.removeAllItems();
        for (Object object : objects) {
            combo.addItem(object);
        }
        combo.setSelectedItem(selected);
    }

    /**
     * Creates new form JTIOADialog
     */
    public JTIOADialog(TIOA tioa) {
        this.origSystem = tioa;
        this.system = (TIOA)tioa.clone();
        
        //repository = repositoryOrig.clone();
        repository = system.getRepository();
        initComponents();
        messages = repository.getAllMessages();
        locations = repository.getAllLocations();
        transitions = repository.getAllTransitions();
//        messages = new LinkedList<Message>(Repository.getInstance().getAllMessages());
//        locations = new LinkedList<Location>(Repository.getInstance().getAllLocations());
//        transitions = new LinkedList<Transition>(Repository.getInstance().getAllTransitions());
        restore();
    }

    private String getSystemName() {
        return jSystemNameTextField.getText();
    }

//    private Location getInitialLocation() {
//        //return 
//    }
    private Clock getSelectedClock() {
        return (Clock) jClocksComboBox.getSelectedItem();
    }

    private Message getSelectedMessage() {
        return (Message) jMessagesComboBox.getSelectedItem();
    }

    private Location getSelectedLocation() {
        return (Location) jLocationsComboBox.getSelectedItem();
    }

    private Transition getSelectedTransition() {
        return (Transition) jTransitionsComboBox.getSelectedItem();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jFramePanel = new javax.swing.JPanel();
        jMessageTypePanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jSystemNameTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jInitialLocationLabel = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLocationsComboBox = new javax.swing.JComboBox();
        jTransitionsComboBox = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jClocksComboBox = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        jTransitionsNewButton = new javax.swing.JButton();
        jTransitionsEditButton = new javax.swing.JButton();
        jTransitionsDeleteButton = new javax.swing.JButton();
        jMessageLabel = new javax.swing.JLabel();
        jMessageButtonsPanel = new javax.swing.JPanel();
        jMessageNewButton = new javax.swing.JButton();
        jMessageEditButton = new javax.swing.JButton();
        jMessageDeleteButton = new javax.swing.JButton();
        jMessagesComboBox = new javax.swing.JComboBox();
        jClockButtonsPanel = new javax.swing.JPanel();
        jClockNewButton = new javax.swing.JButton();
        jClockEditButton = new javax.swing.JButton();
        jClockDeleteButton = new javax.swing.JButton();
        jLocationButtonsPanel = new javax.swing.JPanel();
        jLocationNewButton = new javax.swing.JButton();
        jLocationEditButton = new javax.swing.JButton();
        jLocationDeleteButton = new javax.swing.JButton();
        jButtonsPanel = new javax.swing.JPanel();
        jOKButton = new javax.swing.JButton();
        jResetButton = new javax.swing.JButton();
        jCancelButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();

        setMinimumSize(new java.awt.Dimension(800, 300));
        setModal(true);
        setPreferredSize(new java.awt.Dimension(800, 300));
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jFramePanel.setLayout(new java.awt.GridBagLayout());

        jMessageTypePanel.setLayout(new java.awt.GridLayout(1, 0));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 4;
        jFramePanel.add(jMessageTypePanel, gridBagConstraints);

        jLabel1.setText("System Name ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        jFramePanel.add(jLabel1, gridBagConstraints);

        jSystemNameTextField.setColumns(10);
        jSystemNameTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jSystemNameTextFieldActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        jFramePanel.add(jSystemNameTextField, gridBagConstraints);

        jLabel2.setText("Initial Location");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        jFramePanel.add(jLabel2, gridBagConstraints);

        jInitialLocationLabel.setText("---");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        jFramePanel.add(jInitialLocationLabel, gridBagConstraints);

        jLabel4.setText("Locations");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        jFramePanel.add(jLabel4, gridBagConstraints);

        jLocationsComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jFramePanel.add(jLocationsComboBox, gridBagConstraints);

        jTransitionsComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 10;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jFramePanel.add(jTransitionsComboBox, gridBagConstraints);

        jLabel5.setText("Transitions");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 10;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        jFramePanel.add(jLabel5, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.ipadx = 20;
        jFramePanel.add(jLabel6, gridBagConstraints);

        jLabel7.setText("Clocks");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        jFramePanel.add(jLabel7, gridBagConstraints);

        jClocksComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jFramePanel.add(jClocksComboBox, gridBagConstraints);

        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        jTransitionsNewButton.setText("New");
        jTransitionsNewButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTransitionsNewButtonActionPerformed(evt);
            }
        });
        jPanel2.add(jTransitionsNewButton);

        jTransitionsEditButton.setText("Edit");
        jTransitionsEditButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTransitionsEditButtonActionPerformed(evt);
            }
        });
        jPanel2.add(jTransitionsEditButton);

        jTransitionsDeleteButton.setText("Delete");
        jTransitionsDeleteButton.setEnabled(false);
        jTransitionsDeleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTransitionsDeleteButtonActionPerformed(evt);
            }
        });
        jPanel2.add(jTransitionsDeleteButton);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 10;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        jFramePanel.add(jPanel2, gridBagConstraints);

        jMessageLabel.setText("Message");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        jFramePanel.add(jMessageLabel, gridBagConstraints);

        jMessageButtonsPanel.setLayout(new java.awt.GridLayout(1, 0));

        jMessageNewButton.setText("New");
        jMessageNewButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMessageNewButtonActionPerformed(evt);
            }
        });
        jMessageButtonsPanel.add(jMessageNewButton);

        jMessageEditButton.setText("Edit");
        jMessageEditButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMessageEditButtonActionPerformed(evt);
            }
        });
        jMessageButtonsPanel.add(jMessageEditButton);

        jMessageDeleteButton.setText("Delete");
        jMessageDeleteButton.setEnabled(false);
        jMessageDeleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMessageDeleteButtonActionPerformed(evt);
            }
        });
        jMessageButtonsPanel.add(jMessageDeleteButton);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        jFramePanel.add(jMessageButtonsPanel, gridBagConstraints);

        jMessagesComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jMessagesComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMessagesComboBoxActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jFramePanel.add(jMessagesComboBox, gridBagConstraints);

        jClockButtonsPanel.setLayout(new java.awt.GridLayout(1, 0));

        jClockNewButton.setText("New");
        jClockNewButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jClockNewButtonActionPerformed(evt);
            }
        });
        jClockButtonsPanel.add(jClockNewButton);

        jClockEditButton.setText("Edit");
        jClockEditButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jClockEditButtonActionPerformed(evt);
            }
        });
        jClockButtonsPanel.add(jClockEditButton);

        jClockDeleteButton.setText("Delete");
        jClockDeleteButton.setEnabled(false);
        jClockButtonsPanel.add(jClockDeleteButton);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        jFramePanel.add(jClockButtonsPanel, gridBagConstraints);

        jLocationButtonsPanel.setLayout(new java.awt.GridLayout(1, 0));

        jLocationNewButton.setText("New");
        jLocationNewButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jLocationNewButtonActionPerformed(evt);
            }
        });
        jLocationButtonsPanel.add(jLocationNewButton);

        jLocationEditButton.setText("Edit");
        jLocationEditButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jLocationEditButtonActionPerformed(evt);
            }
        });
        jLocationButtonsPanel.add(jLocationEditButton);

        jLocationDeleteButton.setText("Delete");
        jLocationDeleteButton.setEnabled(false);
        jLocationButtonsPanel.add(jLocationDeleteButton);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        jFramePanel.add(jLocationButtonsPanel, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        getContentPane().add(jFramePanel, gridBagConstraints);

        jButtonsPanel.setLayout(new java.awt.GridLayout(1, 0));

        jOKButton.setText("OK");
        jOKButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jOKButtonActionPerformed(evt);
            }
        });
        jButtonsPanel.add(jOKButton);

        jResetButton.setText("Reset");
        jResetButton.setEnabled(false);
        jResetButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jResetButtonActionPerformed(evt);
            }
        });
        jButtonsPanel.add(jResetButton);

        jCancelButton.setText("Cancel");
        jCancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCancelButtonActionPerformed(evt);
            }
        });
        jButtonsPanel.add(jCancelButton);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        getContentPane().add(jButtonsPanel, gridBagConstraints);

        jPanel1.setPreferredSize(new java.awt.Dimension(100, 30));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 30, Short.MAX_VALUE)
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        getContentPane().add(jPanel1, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTransitionsNewButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTransitionsNewButtonActionPerformed
        // TODO add your handling code here:
        TransitionObservable observable = new TransitionObservable(this, TransitionObservable.MODIF_TYPE.TRANSITION);
        JTransitionDialog frame = new JTransitionDialog(observable, repository);
        frame.setVisible(true);
    }//GEN-LAST:event_jTransitionsNewButtonActionPerformed

    private void jOKButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jOKButtonActionPerformed
        // TODO add your handling code here:
        system.setName(getSystemName());
        //Synchronizing Messages
        for (Message message : messages) {
            repositoryOrig.synchronize(message);
        }
        //Synchronizing Clocks
        for (Clock clock : clocks) {
            repositoryOrig.synchronize(clock);
        }
        //Synchronizing Locations
        for (Location location : locations) {
            repositoryOrig.synchronize(location);
        }
        //Synchronizing Messages
        for (Transition transition : transitions) {
            repositoryOrig.synchronize(transition);
        }
        origSystem = system;
    }//GEN-LAST:event_jOKButtonActionPerformed

    private void jResetButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jResetButtonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jResetButtonActionPerformed

    private void jTransitionsEditButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTransitionsEditButtonActionPerformed
        // TODO add your handling code here:
        TransitionObservable observable = new TransitionObservable(this, TransitionObservable.MODIF_TYPE.TRANSITION);
        JTransitionDialog frame = new JTransitionDialog(getSelectedTransition(), observable, repository);
        frame.setVisible(true);
    }//GEN-LAST:event_jTransitionsEditButtonActionPerformed

    private void jTransitionsDeleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTransitionsDeleteButtonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTransitionsDeleteButtonActionPerformed

    private void jSystemNameTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jSystemNameTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jSystemNameTextFieldActionPerformed

    private void jMessageNewButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMessageNewButtonActionPerformed
        // TODO add your handling code here:
        TransitionObservable observable = new TransitionObservable(this, TransitionObservable.MODIF_TYPE.MESSAGE);
        JMessageDialog frame = new JMessageDialog(observable, messages);
        frame.setVisible(true);
    }//GEN-LAST:event_jMessageNewButtonActionPerformed

    private void jMessageEditButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMessageEditButtonActionPerformed
        // TODO add your handling code here:
//////        transition.setMessage(getSelectedMessage());
//////        TransitionObservable transitionObservable = new TransitionObservable(this);
//////        JMessageDialog frame = new JMessageDialog(getSelectedMessage(), transitionObservable, messages);
//////        frame.setVisible(true);
        TransitionObservable observable = new TransitionObservable(this, TransitionObservable.MODIF_TYPE.MESSAGE);
        JMessageDialog frame = new JMessageDialog(getSelectedMessage(), observable, messages);
        frame.setVisible(true);
    }//GEN-LAST:event_jMessageEditButtonActionPerformed

    private void jMessageDeleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMessageDeleteButtonActionPerformed
        // TODO add your handling code here:
////        Message msg = getSelectedMessage();
////        if (!msg.equals(internalAction)) {
////            transition.setMessage(internalAction);
////        }
////        update(null, null);
    }//GEN-LAST:event_jMessageDeleteButtonActionPerformed

    private void jMessagesComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMessagesComboBoxActionPerformed
        // TODO add your handling code here:
////        if (transition != null) {
////            transition.setMessage(getSelectedMessage());
////        }
        if (getSelectedMessage() != null) {
            jMessageEditButton.setEnabled(!getSelectedMessage().equals(internalAction));
        }
    }//GEN-LAST:event_jMessagesComboBoxActionPerformed

    private void jLocationNewButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jLocationNewButtonActionPerformed
        // TODO add your handling code here:
        TransitionObservable observable = new TransitionObservable(this, TransitionObservable.MODIF_TYPE.LOCATION);
        JLocationDialogue frame = new JLocationDialogue(observable, locations);
        frame.setVisible(true);
    }//GEN-LAST:event_jLocationNewButtonActionPerformed

    private void jLocationEditButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jLocationEditButtonActionPerformed
        // TODO add your handling code here:
////        transition.setDestLocation(getSelectedDestLocation());
////        TransitionObservable transitionObservable = new TransitionObservable(this);
////        JLocationFrame frame = new JLocationFrame(getSelectedDestLocation(), transitionObservable, locations);
////        frame.setVisible(true);
        TransitionObservable observable = new TransitionObservable(this, TransitionObservable.MODIF_TYPE.LOCATION);
        JLocationDialogue frame = new JLocationDialogue(getSelectedLocation(), observable, locations);
        frame.setVisible(true);
    }//GEN-LAST:event_jLocationEditButtonActionPerformed

    private void jClockNewButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jClockNewButtonActionPerformed
        // TODO add your handling code here:
        TransitionObservable observable = new TransitionObservable(this, TransitionObservable.MODIF_TYPE.CLOCK);
        JClockDialogue frame = new JClockDialogue(observable, clocks);
        frame.setVisible(true);
    }//GEN-LAST:event_jClockNewButtonActionPerformed

    private void jClockEditButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jClockEditButtonActionPerformed
        // TODO add your handling code here:
        TransitionObservable observable = new TransitionObservable(this, TransitionObservable.MODIF_TYPE.CLOCK);
        JClockDialogue frame = new JClockDialogue(getSelectedClock(), observable, clocks);
        frame.setVisible(true);
    }//GEN-LAST:event_jClockEditButtonActionPerformed

    private void jCancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCancelButtonActionPerformed
        // TODO à modifier:
        system = origSystem;
        System.exit(0);
    }//GEN-LAST:event_jCancelButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JTIOADialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JTIOADialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JTIOADialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JTIOADialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JTIOADialog(null).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jButtonsPanel;
    private javax.swing.JButton jCancelButton;
    private javax.swing.JPanel jClockButtonsPanel;
    private javax.swing.JButton jClockDeleteButton;
    private javax.swing.JButton jClockEditButton;
    private javax.swing.JButton jClockNewButton;
    private javax.swing.JComboBox jClocksComboBox;
    private javax.swing.JPanel jFramePanel;
    private javax.swing.JLabel jInitialLocationLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jLocationButtonsPanel;
    private javax.swing.JButton jLocationDeleteButton;
    private javax.swing.JButton jLocationEditButton;
    private javax.swing.JButton jLocationNewButton;
    private javax.swing.JComboBox jLocationsComboBox;
    private javax.swing.JPanel jMessageButtonsPanel;
    private javax.swing.JButton jMessageDeleteButton;
    private javax.swing.JButton jMessageEditButton;
    private javax.swing.JLabel jMessageLabel;
    private javax.swing.JButton jMessageNewButton;
    private javax.swing.JPanel jMessageTypePanel;
    private javax.swing.JComboBox jMessagesComboBox;
    private javax.swing.JButton jOKButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JButton jResetButton;
    private javax.swing.JTextField jSystemNameTextField;
    private javax.swing.JComboBox jTransitionsComboBox;
    private javax.swing.JButton jTransitionsDeleteButton;
    private javax.swing.JButton jTransitionsEditButton;
    private javax.swing.JButton jTransitionsNewButton;
    // End of variables declaration//GEN-END:variables

    
    @Override
    public void update(Observable o, Object o1) {
        TransitionObservable obs = (TransitionObservable) o;
        switch (obs.getModifType()) {
            case MESSAGE: {
                message = (Message)(obs.getResult());
                repository.synchronize(message);
                //transition.setMessage((Message) (obs.getResult()));
                break;
            }
            case LOCATION: {
                location = (Location)(obs.getResult());
                repository.synchronize(location);
                //transition.setSrcLocation((Location) (obs.getResult()));
                break;
            }
            case CLOCK: {
                clock = (Clock)(obs.getResult());
                repository.synchronize(clock);
                //transition.set((Location)(obs.getResult()));                
                break;
            }
            case TRANSITION: {
                transition = (Transition) obs.getResult();
                repository.synchronize(transition);
                ReachabilityAnalysis analysis = new IncrementalReachabilityAnalysis();
                TIOARuntime tioaRuntime = analysis.analyse(system);
                //system.reachabilityAnalysis();
                System.out.println("TIOA");
                System.out.println(tioaRuntime.printReachability());
//                System.out.println(transition);
//                System.out.println("\n==============\n");
//                System.out.println(repository.print());
            }
        }
        fillInComponents();

    }
}
